//
//  ViewController.m
//  02_寄宿图
//
//  Created by apple on 16/5/26.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *layerView;

@property (weak, nonatomic) IBOutlet UIView *view1;

@property (weak, nonatomic) IBOutlet UIView *view2;

@property (weak, nonatomic) IBOutlet UIView *view3;

@property (weak, nonatomic) IBOutlet UIView *view4;

@property (weak, nonatomic) IBOutlet UIView *layerView2;

@property (weak, nonatomic) IBOutlet UIView *layerView3;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self attributeOfConstents];
    
    [self attributeOfContentsRect];
    
    [self attributeOfContentsCenter];
    
    [self attributeOfCustomeDrawing];
    
    
}

//constents 属性
- (void)attributeOfConstents{

    //向Layer层添加图片
    //属性类型为ID 类型 , 但只能接受CGImageRef类型数据.(image.CGimage)
    //但CGImageRef 是Core Foundation类型 , 赋值时需用(__bridge id)强转

    
    UIImage *image = [UIImage imageNamed:@"pic_s1.jpg"];
    
    //但好像 只用(id)进行强转也可以 ??????
    self.layerView.layer.contents = (__bridge id)image.CGImage;
    
    
    //设置对齐方式(两者效果一样)
    /**枚举值
     kCAGravityCenter
     kCAGravityTop
     kCAGravityBottom
     kCAGravityLeft
     kCAGravityRight
     kCAGravityTopLeft
     kCAGravityTopRight
     kCAGravityBottomLeft
     kCAGravityBottomRight
     kCAGravityResize
     kCAGravityResizeAspect
     kCAGravityResizeAspectFill*/
    self.layerView.contentMode = UIViewContentModeScaleAspectFit;
    
    self.layerView.layer.contentsGravity = kCAGravityCenter;
    
    
    //contentsScale 属性 , 定义了寄宿图的像素尺寸和视图大小的比例.(默1.0f)
    //每个点 几个像素绘制图片.
    
    //通过 contentsScale属性 , 确保图片拉伸时 不失真.
    self.layerView.layer.contentsScale = image.scale;
    self.layerView.layer.contentsScale = [UIScreen mainScreen].scale;
    
    //makeToBounds
    //是否截取边界.
    self.layerView.layer.masksToBounds = YES;
    /**与UIView 层 self.view.clipsToBounds 效果相同*/

    
}


- (void)attributeOfContentsRect{

    //contentsRect
    //根据指定的Rect 来截取Layer层图片(整体:{0,0,1,1})
    
    UIImage *iamge = [UIImage imageNamed:@"pic_s1.jpg"];
    
    self.view1.layer.contents = (id)iamge.CGImage;
    self.view1.layer.contentsRect = CGRectMake(0, 0, 0.5, 0.5);
    self.view1.layer.contentsScale = 4;
    
    self.view2.layer.contents = (id)iamge.CGImage;
    self.view2.layer.contentsRect = CGRectMake(0.5, 0, 0.5, 0.5);
    
    
    self.view3.layer.contents = (id)iamge.CGImage;
    self.view3.layer.contentsRect = CGRectMake(0, 0.5, 0.5, 0.5);
    
    self.view4.layer.contents = (id)iamge.CGImage;
    self.view4.layer.contentsRect = CGRectMake(0.5, 0.5, 0.5, 0.5);
    
    
}


- (void)attributeOfContentsCenter{

    //属性 : ContentsCenter
    //固定的边框 和 可拉伸的区域(默{0,0,1,1})
    //增加原点的值并减小尺寸。会在图片的周围创造一个边框
    //当视图被拉伸时  , 保证图片的四个角 , 不被拉伸(不变形)!!!!!.
    
//    self.view1.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);
    
    UIImage *image = [UIImage imageNamed:@"test.jpg"];
    
    self.layerView3.layer.contents = (id)image.CGImage;
    
    self.layerView3.layer.contentsCenter = CGRectMake(0.15, 0.15, 0.7, 0.7);
    

}


- (void)attributeOfCustomeDrawing{

    CALayer *bluelayer = [CALayer layer];
    bluelayer.frame = CGRectMake(50, 50, 100, 100);
    bluelayer.backgroundColor = [UIColor blueColor].CGColor;
    
    //设置代理
    bluelayer.delegate = self;
    
    bluelayer.contentsScale = [UIScreen mainScreen].scale;
    [self.layerView2.layer addSublayer:bluelayer];
    
    //更新图层的内容
    [bluelayer display];
    
}


//当图形需要被重绘时 调用 (void)displayLayer:(CALayerCALayer *)layer;
//如果上述方法未被实现 , 将调用下列代码
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{

    //CGContextRef 为上下文
    CGContextSetLineWidth(ctx, 10.0f);
    //边框颜色
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    CGContextStrokeEllipseInRect(ctx, layer.bounds);
    

}



@end
